package com.hctforgreen.greenservice.ui.widget;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.hctforgreen.greenservice.MessageFirstActivityV2;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.MessageTopFirstEntity;
import com.hctforgreen.greenservice.ui.adapter.MessageTopFirstLstAdapter;

public class MessageTopFirstPopupView extends PopupWindow {

	private View mConvertView;
	private Activity mActivity;

	private MessageTopFirstEntity mCurrentFirstEntity;
	private ArrayList<MessageTopFirstEntity> mEntities = new ArrayList<MessageTopFirstEntity>();

	private MessageFirstActivityV2 mSelf;

	public MessageTopFirstPopupView(Activity activity,
			MessageTopFirstEntity currentFirstEntity,
			MessageFirstActivityV2 self) {
		super(activity);
		mActivity = activity;

		mCurrentFirstEntity = currentFirstEntity;
		mSelf = self;
		initDataLst();
		initWindow();
	}

	private void initWindow() {
		mConvertView = getLayout();

		this.setContentView(mConvertView);
		this.setBackgroundDrawable(new BitmapDrawable());
		this.setOutsideTouchable(true);
		this.setFocusable(true);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);

		loadDataLst();
	}

	private void initDataLst() {
		MessageTopFirstEntity entity = new MessageTopFirstEntity();
		entity.name = mActivity.getString(R.string.message_top_first_01_hint);
		entity.type = "1";
		mEntities.add(entity);

		entity = new MessageTopFirstEntity();
		entity.name = mActivity.getString(R.string.message_top_first_02_hint);
		entity.type = "2";
		mEntities.add(entity);

		entity = new MessageTopFirstEntity();
		entity.name = mActivity.getString(R.string.message_top_first_03_hint);
		entity.type = "3";
		mEntities.add(entity);

//		entity = new MessageTopFirstEntity();
//		entity.name = mActivity.getString(R.string.message_top_first_04_hint);
//		entity.type = "4";
//		mEntities.add(entity);
	}

	/**
	 * 获取布局文件view
	 * 
	 * @return
	 */
	private View getLayout() {
		View layout = mActivity.getLayoutInflater().inflate(
				R.layout.popup_message_top_list, null);
		return layout;
	}

	private void loadDataLst() {
		final ListView firstLv = (ListView) mConvertView
				.findViewById(R.id.lst_default_list);

		renderDataLst(firstLv, mEntities);

	}

	private void renderDataLst(ListView firstLv,
			List<MessageTopFirstEntity> entities) {

		if (entities == null || entities.size() == 0) {
			return;
		}
		new MessageTopFirstLstAdapter(firstLv, mActivity, entities,
				mCurrentFirstEntity, this);
	}

	public void updateWindow(MessageTopFirstEntity currentFirstEntity) {
		mSelf.updateFirstEntity(currentFirstEntity);
		mSelf.updateFirstBtn(currentFirstEntity);
	}

}
